<?php
require_once './Classes/PHPExcel.php';   
// 创建一个处理对象实例   

$objExcel = new PHPExcel();   

  

// 创建文件格式写入对象实例, uncomment   

$objWriter = new PHPExcel_Writer_Excel5($objExcel);     // 用于其他版本格式   

//设置文档基本属性   

$objProps = $objExcel->getProperties();   

$objProps->setCreator("Zeal Li");   

$objProps->setLastModifiedBy("Zeal Li");   

$objProps->setTitle("Office XLS Test Document");   

$objProps->setSubject("Office XLS Test Document, Demo");   

$objProps->setDescription("Test document, generated by PHPExcel.");   

$objProps->setKeywords("office excel PHPExcel");   

$objProps->setCategory("Test");   

  


$objExcel->setActiveSheetIndex(0);   


$objActSheet = $objExcel->getActiveSheet();   

  

//设置当前活动sheet的名称   

$objActSheet->setTitle('sheet1');   

  

//*************************************   

//设置单元格内容   

//   
$arr = array(
	0 => array('id'=>'1','name'=>'zhangsan','pwd'=>'1223334343434343434343222'),
	1 => array('id'=>'2','name'=>'lisi','pwd'=>'xxx'),
	2 => array('id'=>'3','name'=>'wangwu','pwd'=>'122www33'),
	3 => array('id'=>'4','name'=>'liliu','pwd'=>'0000'),

);
//由PHPExcel根据传入内容自动判断单元格内容类型  
$nums = array('1' => 'A','2' => 'B',3=>'C','4'=>'D');
$i = 1;
foreach($arr as $k => $v){
	$j=1;
	foreach($v as $k1=>$v1){
		echo $v1;
		//
		$objActSheet->setCellValue($nums[$j] . $i, $v1);  // 
		$j++;
	}
	$i++;
}

//合并单元格
//$objActSheet->mergeCells('A2:C2');
//$objStyleA1 = $objActSheet->getStyle('A2');
//设置居中
//$objStyleA1->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置格式为字符串
//$objActSheet->setCellValueExplicit($nums[$j] . $i, ' '.$v1,PHPExcel_Cell_DataType::TYPE_STRING);  // 字符串内容  
//设置宽度   

$objActSheet->getColumnDimension('B')->setAutoSize(true);   

$objActSheet->getColumnDimension('A')->setWidth(30);   

//输出内容   

//   

$outputFileName = "output.xls";   

//到文件   

$objWriter->save('./test.xls');   
exit;

//或者直接浏览器下载   (任选其一)
//------------------------------------
$outputFileName = "output.xls"; 
header("Content-Type:application/octet-stream;charset=utf-8");
header('Content-Disposition: attachment; filename=' . $outputFileName); 
$objWriter->save('php://output');
//------------------------------------ 


?>   
 
